package br.com.amadeus.android.activities.course.task;

import java.util.ArrayList;

import br.com.amadeus.android.activities.home.R;
import br.com.amadeus.android.adapters.GoalAdapter;
import br.com.amadeus.android.adapters.GoalInTaskAdapter;
import br.com.amadeus.android.adapters.GoalList;
import br.com.amadeus.android.business.FacadeAmadeusDroid;
import br.com.amadeus.android.entities.Course;
import br.com.amadeus.android.entities.CourseUser;
import br.com.amadeus.android.entities.Goal;
import android.app.Activity;
import android.app.AlarmManager;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.ImageButton;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.Toast;
import android.widget.AdapterView.OnItemClickListener;

 

public class HomeTaskActivity extends Activity{
	
	private int course_id;
	private Intent intent;
	private FacadeAmadeusDroid facade = FacadeAmadeusDroid.getInstance(this);
	private ListView lvGoal;
	
	public void onCreate(Bundle savedInstanceState){ 
		super.onCreate(savedInstanceState);
        setContentView(R.layout.amadeus_home_task);
    	//video de libras
		ImageButton librasBt = (ImageButton)findViewById(R.id.botaoLibras7);
        librasBt.setOnClickListener(new OnClickListener() {
			
			public void onClick(View arg0) {
				Intent it = new Intent("ABRIR_LIBRAS");
				it.putExtra("video_libras", "HomeTask.wmv");
				startActivity(it);
				
			}
		});
        
    	Bundle basket = getIntent().getExtras();
    	int goal_id = 0;
    	goal_id = basket.getInt("goal_id");
    	if (goal_id == 0) {
    		course_id = basket.getInt("course_id");
		}else{
			Goal goal = facade.getGoal(goal_id);
			CourseUser courseUser = facade.getCourseUserById(goal.getCourseUser().getId());
			Course course = facade.getCourseByAmadeusId(courseUser.getCourse().getId());
			course_id = course.getId();
		}
        
        ArrayList<Goal> goals = facade.getAllGoalByCourse(course_id);
        if( goals!= null ){
        	//Listar Goals em um Adapter
        	AlarmManager alarme = (AlarmManager) getSystemService(ALARM_SERVICE);
            GoalInTaskAdapter goalAdapter = new GoalInTaskAdapter(this, goals);

            this.lvGoal = (ListView) findViewById(R.id.lvGoals);
            this.lvGoal.setAdapter(goalAdapter);
            this.lvGoal.setOnItemClickListener(new OnItemClickListener() {

    			public void onItemClick(AdapterView<?> parent, View v, int posicao, long id) {
    				intent = new Intent("LIST_TASK");
    				Goal goal = (Goal) lvGoal.getAdapter().getItem(posicao);
    				Bundle basket = new Bundle();
    				basket.putInt("goal_id", goal.getId());
    				basket.putInt("course_id", course_id);
    				intent.putExtras(basket);
    				startActivity(intent);
    			}
    		});
        }else{
        	AlertDialog.Builder dialog = new AlertDialog.Builder(this);
			dialog.setMessage("Primeiro você deve cadastrar um Objetivo para depois cadastrar uma Tarefa.");
			dialog.setNeutralButton("OK", new DialogInterface.OnClickListener() {
				public void onClick(DialogInterface dialog, int which) {
					dialog.dismiss();
					try {
						this.finalize();
					} catch (Throwable e) {
						e.printStackTrace();
					}
					
				}
			});
			dialog.show();
        }
        
    }

	@Override
	protected void onStop() {
		// TODO Auto-generated method stub
		super.onStop();
		finish();
	}
	
	
}
